<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Java 故障诊断实战, Java,Linux,Mysql,Redis,HTTP,操作系统,计算机网络,计算机组成原理,数据结构与算法,工具使用,性能优化">
    <meta name="description" content="本网站是个人兴趣爱好，总结分享经验，记录生活点滴的平台，希望在以后的学习旅途中，走出自己的风景。">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>Java 故障诊断实战 | 八戒取经路</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.4.0">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="alternate" href="/atom.xml" title="八戒取经路" type="application/atom+xml">
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">八戒取经路</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/navigate" class="waves-effect waves-light">
      
      <i class="fas fa-location-arrow" style="zoom: 0.6;"></i>
      
      <span>导航</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">

      
      <i class="fas fa-address-card" style="zoom: 0.6;"></i>
      
      <span>关于</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/about">
          
          <i class="fas fa-user-circle" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>关于我</span>
        </a>
      </li>
      
      <li>
        <a href="/aboutme">
          
          <i class="fa fa-user-secret" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>Another</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">八戒取经路</div>
        <div class="logo-desc">
            
            本网站是个人兴趣爱好，总结分享经验，记录生活点滴的平台，希望在以后的学习旅途中，走出自己的风景。
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/navigate" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-location-arrow"></i>
			
			导航
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-address-card"></i>
			
			关于
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/about " style="margin-left:75px">
				  
				   <i class="fa fas fa-user-circle" style="position: absolute;left:50px" ></i>
			      
		          <span>关于我</span>
                  </a>
                </li>
              
                <li>

                  <a href="/aboutme " style="margin-left:75px">
				  
				   <i class="fa fa fa-user-secret" style="position: absolute;left:50px" ></i>
			      
		          <span>Another</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/2.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">Java 故障诊断实战</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Java/">
                                <span class="chip bg-color">Java</span>
                            </a>
                        
                            <a href="/tags/JVM/">
                                <span class="chip bg-color">JVM</span>
                            </a>
                        
                            <a href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
                                <span class="chip bg-color">性能优化</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2021-04-17
                </div>
                

                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    4.8k
                </div>
                

                

                
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>本文基于 <code>Java8</code>。</p>
<h2 id="JVM参数类型"><a href="#JVM参数类型" class="headerlink" title="JVM参数类型"></a>JVM参数类型</h2><p>JVM的参数基本可以分为以下三种类型：</p>
<h3 id="标准参数"><a href="#标准参数" class="headerlink" title="标准参数"></a>标准参数</h3><p>在各个版本中基本不会发生变化的标准参数，比如有：</p>
<ul>
<li><code>-help</code>：查看帮助；</li>
<li><code>-server</code>：切换为server模式；</li>
<li><code>-client</code>：切换为client模式；</li>
<li><code>-version</code>：查看JDK版本；</li>
<li><code>-showversion</code>：输出产品版本并继续。</li>
</ul>
<p>更多标准参数可以通过<code>java -help</code>查看。</p>
<h3 id="X参数"><a href="#X参数" class="headerlink" title="X参数"></a>X参数</h3><p>X参数也称为非标准化参数，在各个JDK版本中可能存在差异，比如有：</p>
<ul>
<li><code>-Xint</code>：解释执行；</li>
<li><code>-Xcomp</code>：第一次使用就编译成本地代码；</li>
<li><code>-Xmixed</code>：混合模式，JVM自己来决定是否编译成本地代码。</li>
</ul>
<h3 id="XX参数"><a href="#XX参数" class="headerlink" title="XX参数"></a>XX参数</h3><p>XX也是非标准化参数，主要用于JVM调优和问题定位，XX参数分为两大类型：</p>
<ol>
<li>Boolean类型，格式：<code>-XX:[+-]&lt;name&gt;</code>标识启用或禁用name属性，比如：<ul>
<li><code>-XX:+PrintGCDetails</code></li>
<li><code>-XX:+UseG1GC</code></li>
</ul>
</li>
<li>非Boolean类型（key-value形式），格式：<code>-XX:&lt;name&gt;=&lt;value&gt;</code>表示name属性的值是value，比如：<ul>
<li><code>-XX:MaxGCPauseMillis=500</code></li>
<li><code>-XX:GCTimeRatio=19</code></li>
</ul>
</li>
</ol>
<h2 id="运行时JVM参数查看"><a href="#运行时JVM参数查看" class="headerlink" title="运行时JVM参数查看"></a>运行时JVM参数查看</h2><ul>
<li><code>-XX:+PrintFlagsInitial</code>：查看参数初始值，=表示默认值，:=表示修改后的值。</li>
<li><code>-XX:+PrintFlagsFinal</code>：查看参数最终值。</li>
<li><code>-XX:+UnlockExperimentalVMOptions</code>：解锁实验参数。</li>
<li><code>-XX:+UnlockDiagnosticVMOptions</code>：解锁诊断参数。</li>
<li><code>-XX:+PrintCommandLineFlags</code>：打印命令行参数。</li>
</ul>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-142410@2x.png" alt="QQ20201109-100752@2x"><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-142410@2x.png" alt="QQ20201109-100904@2x"><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-142410@2x.png" alt="QQ20201109-100935@2x"></p>
<p>上面这些命令查看的参数值对应的进程为执行当前命令进程，如果需要查看某个运行时Java应用相关参数，则可以使用下面这些命令：</p>
<ul>
<li><code>jps</code>：查看Java进程ID，<code>-l</code>显示完整启动类名。</li>
<li><code>jinfo</code>：查看运行时Java程序相关参数值，使用方式<code>jinfo -flag [参数名] [进程ID]</code>。</li>
</ul>
<p>比如当前有一个正常运行的boot-app.jar，查看其最大堆内存：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-142410@2x.png" alt="QQ20201109-103454@2x"></p>
<h2 id="jstat统计JVM信息"><a href="#jstat统计JVM信息" class="headerlink" title="jstat统计JVM信息"></a>jstat统计JVM信息</h2><p><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html">jstat</a>可以监控许多JVM信息，下面逐一演示，演示基于boot-app.jar，进程号为4425。</p>
<h3 id="class"><a href="#class" class="headerlink" title="class"></a>class</h3><p>监控类加载器行为信息：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-142410@2x.png" alt="QQ20201109-141804@2x"></p>
<p>各列含义如下：</p>
<ul>
<li>Loaded：已加载类数量；</li>
<li>Bytes：已加载类大小，单位为KB；</li>
<li>Unloaded：已卸载类数量；</li>
<li>Bytes：已卸载类大小，单位为KB；</li>
<li>Time：执行类加载和卸载所花费的时间。</li>
</ul>
<h3 id="compiler"><a href="#compiler" class="headerlink" title="compiler"></a>compiler</h3><p>监控HotSpot VM实时编译器信息：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-142410@2x.png" alt="QQ20201109-142410@2x"></p>
<ul>
<li>Compiled：执行的编译任务数量；</li>
<li>Failed：编译失败任务数量；</li>
<li>Invalid：无效的编译数量；</li>
<li>Time：编译耗时；</li>
<li>FailedType：上次编译失败的类型；</li>
<li>FailedMethod：编译失败的类名和方法。</li>
</ul>
<h3 id="gc"><a href="#gc" class="headerlink" title="gc"></a>gc</h3><p>统计堆区GC信息：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-145452@2x.png" alt="QQ20201109-143447@2x"></p>
<ul>
<li>S0C：当前幸存者0区大小，单位KB；</li>
<li>S1C：当前幸存者1区大小，单位KB；</li>
<li>S0U：幸存者0区已使用内存大小，单位KB；</li>
<li>S1U：幸存者1区已使用内存大小，单位KB；</li>
<li>EC：伊甸园区大小，单位KB；</li>
<li>EU：伊甸园区已使用内存大小，单位KB；</li>
<li>OC：老年代内存大小，单位KB；</li>
<li>OU：老年代已使用内存大小，单位KB；</li>
<li>MC：元空间大小，单位KB；</li>
<li>MU：元空间已使用内存大小，单位KB；</li>
<li>CCSC：压缩类空间大小，单位KB；</li>
<li>CCSU：压缩类空间已使用内存大小，单位KB；</li>
<li>YGC：YGC次数；</li>
<li>YGCT：YGC耗时；</li>
<li>FGC：FGC次数；</li>
<li>FGCT：FGC耗时；</li>
<li>GCT：GC总耗时。</li>
</ul>
<h3 id="gccapacity"><a href="#gccapacity" class="headerlink" title="gccapacity"></a>gccapacity</h3><p>和GC相关的一些内存大小统计信息：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-145452@2x.png" alt="QQ20201109-145452@2x"></p>
<p>部分列含义和<code>-gc</code>相同，下面仅列出不同部分：</p>
<ul>
<li>NGCMN：新生代最小内存大小，单位KB；</li>
<li>NGCMX：新生代最大内存大小，单位KB；</li>
<li>NGC：新生代当前内存大小，单位KB；</li>
<li>OGCMN：老年代最小内存大小，单位KB；</li>
<li>OGCMX：老年代最大内存大小，单位KB；</li>
<li>MCMN：元空间最小内存大小，单位KB；</li>
<li>MCMX：元空间最大内存大小，单位KB；</li>
<li>CCSMN：压缩类空间最小内存大小，单位KB；</li>
<li>CCSMX：压缩类空间最大内存大小，单位KB。</li>
</ul>
<h3 id="gcutil"><a href="#gcutil" class="headerlink" title="gcutil"></a>gcutil</h3><p>GC信息摘要、总览：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-145452@2x.png" alt="QQ20201109-150548@2x"></p>
<p>部分列含义和<code>-gc</code>相同，下面仅列出不同部分：</p>
<ul>
<li>S0：幸存者0区内存使用率；</li>
<li>S1：幸存者1区内存使用率；</li>
<li>E：伊甸园区内存使用率；</li>
<li>O：老年代内存使用率；</li>
<li>M：元空间内存使用率；</li>
<li>CCS：压缩类空间内存使用率。</li>
</ul>
<h3 id="gccause"><a href="#gccause" class="headerlink" title="gccause"></a>gccause</h3><p>大部分和<code>-gcutil</code>一致，新增了LGCC和GCC：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-145452@2x.png" alt="QQ20201109-160201@2x"></p>
<ul>
<li>LGCC：最后一次GC的原因；</li>
<li>GCC：当前GC原因。</li>
</ul>
<h3 id="gcnew"><a href="#gcnew" class="headerlink" title="gcnew"></a>gcnew</h3><p>一些和新生代GC相关的统计信息：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-145452@2x.png" alt="QQ20201109-160622@2x"></p>
<p>部分列含义和<code>-gc</code>相同，下面仅列出不同部分：</p>
<ul>
<li>TT：Tenuring threshold，即当前对象从新生代晋升到老年代的年龄阈值（JVM动态调整）；</li>
<li>MTT：MaxTenuringThreshold，最大可设晋升阈值（默认值，或者我们手动设置的值）；</li>
<li>DSS：期望的幸存区的大小（从上面截图可以看出，值为21504KB，和幸存者0区、幸存者1区一样大，即DSS的值为当前幸存者区的一半。DSS的计算公式为：当前幸存者区 * TargetSurvivorRatio/100，TargetSurvivorRatio的默认值为50，所以DSS计算出来为当前幸存者区的一半。关于对象从新生代晋升到老年代的细节可以参考这篇博文：<a target="_blank" rel="noopener" href="https://www.cnblogs.com/wangshen31/p/10427802.html%EF%BC%89">https://www.cnblogs.com/wangshen31/p/10427802.html）</a></li>
</ul>
<h3 id="gcnewcapacity"><a href="#gcnewcapacity" class="headerlink" title="gcnewcapacity"></a>gcnewcapacity</h3><p>统计新生代容量相关信息：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-145452@2x.png" alt="QQ20201109-162317@2x"></p>
<p>部分列含义和<code>-gc</code>、<code>-gccapacity</code>相同，下面仅列出不同部分：</p>
<ul>
<li>S0CMX：幸存者0区最大内存，单位KB；</li>
<li>S1CMX：幸存者1区最大内存，单位KB；</li>
<li>ECMX：伊甸园区最大内存，单位KB；</li>
</ul>
<h3 id="gcold"><a href="#gcold" class="headerlink" title="gcold"></a>gcold</h3><p>一些和老年代GC相关统计信息：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163049@2x.png" alt="QQ20201109-162742@2x"></p>
<p>这些列含义都已涉及，不再赘述。</p>
<h3 id="gcoldcapacity"><a href="#gcoldcapacity" class="headerlink" title="gcoldcapacity"></a>gcoldcapacity</h3><p>一些和老年代容量相关统计信息：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163049@2x.png" alt="QQ20201109-163049@2x"></p>
<p>这些列含义都已涉及，不再赘述。</p>
<h3 id="gcmetacapacity"><a href="#gcmetacapacity" class="headerlink" title="gcmetacapacity"></a>gcmetacapacity</h3><p>一些和元空间容量相关统计信息：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163049@2x.png" alt="QQ20201109-163219@2x"></p>
<p>这些列含义都已涉及，不再赘述。</p>
<h3 id="printcompilation"><a href="#printcompilation" class="headerlink" title="printcompilation"></a>printcompilation</h3><p>HotSpot VM编译方法信息统计：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163049@2x.png" alt="QQ20201109-163430@2x"></p>
<ul>
<li>Compiled：近期编译方法数；</li>
<li>Size：近期编译方法字节数；</li>
<li>Type：近期编译方法类型；</li>
<li>Method：近期编译的方法类名和方法名。</li>
</ul>
<p>上面的<code>jstat</code>命令输出的是当前统计信息，我们还可以打印出一段时间内的统计信息，比如每隔1秒输出一次，共输出5次：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163049@2x.png" alt="QQ20201109-163905@2x"></p>
<h2 id="jmap-JProfiler内存溢出实战"><a href="#jmap-JProfiler内存溢出实战" class="headerlink" title="jmap+JProfiler内存溢出实战"></a>jmap+JProfiler内存溢出实战</h2><p><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jmap.html">jmap</a>可以为Java进程打印出内存信息。</p>
<p>Java程序内存溢出一般分为堆内存溢出和元空间内存溢出，内存溢出后，我们可以通过JProfiler（eclipse MAT也行，本文不介绍）软件分析内存映像文件来定位问题所在。</p>
<h3 id="场景构建"><a href="#场景构建" class="headerlink" title="场景构建"></a>场景构建</h3><p>首先模拟堆内存溢出和元空间内存溢出场景。新建一个SpringBoot项目，然后创建一个OutOfMemoryController：</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">import</span> jdk<span class="token punctuation">.</span>internal<span class="token punctuation">.</span>org<span class="token punctuation">.</span>objectweb<span class="token punctuation">.</span>asm<span class="token punctuation">.</span>ClassWriter<span class="token punctuation">;</span>
<span class="token keyword">import</span> jdk<span class="token punctuation">.</span>internal<span class="token punctuation">.</span>org<span class="token punctuation">.</span>objectweb<span class="token punctuation">.</span>asm<span class="token punctuation">.</span>Opcodes<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>GetMapping<span class="token punctuation">;</span>
<span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>RestController<span class="token punctuation">;</span>

<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>ArrayList<span class="token punctuation">;</span>
<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>List<span class="token punctuation">;</span>

<span class="token annotation punctuation">@RestController</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OutOfMemoryController</span> <span class="token keyword">extends</span> <span class="token class-name">ClassLoader</span> <span class="token punctuation">{</span>

    <span class="token keyword">private</span> List<span class="token operator">&lt;</span>User<span class="token operator">></span> data <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator">&lt;</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"heap"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">heapOutOfMemory</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">long</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            i <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span>
            data<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token string">"test"</span> <span class="token operator">+</span> i<span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"metaspace"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">metaspaceOutOfMemory</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        OutOfMemoryController controller <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">OutOfMemoryController</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            i <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span>
            String className <span class="token operator">=</span> <span class="token string">"Class"</span> <span class="token operator">+</span> i<span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">// 创建ClassWriter对象，用于生成类的二进制字节码</span>
            ClassWriter classWriter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassWriter</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">// 指定版本号、修饰符、类名、包名、父类和接口</span>
            classWriter<span class="token punctuation">.</span><span class="token function">visit</span><span class="token punctuation">(</span>Opcodes<span class="token punctuation">.</span>V1_8<span class="token punctuation">,</span> Opcodes<span class="token punctuation">.</span>ACC_PUBLIC<span class="token punctuation">,</span> className<span class="token punctuation">,</span> null<span class="token punctuation">,</span> <span class="token string">"java/lang/Object"</span><span class="token punctuation">,</span> null<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> classWriter<span class="token punctuation">.</span><span class="token function">toByteArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">// 加载类</span>
            controller<span class="token punctuation">.</span><span class="token function">defineClass</span><span class="token punctuation">(</span>className<span class="token punctuation">,</span> bytes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> bytes<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">static</span> <span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token punctuation">{</span>
        <span class="token keyword">private</span> String username<span class="token punctuation">;</span>
        <span class="token keyword">private</span> Long id<span class="token punctuation">;</span>

        <span class="token keyword">public</span> <span class="token function">User</span><span class="token punctuation">(</span>String username<span class="token punctuation">,</span> Long id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>username <span class="token operator">=</span> username<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>在<code>heapOutOfMemory</code>方法中，我们不断创建User对象，并添加到成员变量data集合中，强引用无法得到释放，最终导致堆内存溢出；</p>
<p>在<code>metaspaceOutOfMemory()</code>方法中，我们不断加载新的class，最终元空间也将内存溢出。</p>
<p>为了测试方便，我们通过参数<code>-Xms64m -Xmx64m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=64m</code>限制堆和元空间大小。</p>
<p>启动Boot应用，访问<a target="_blank" rel="noopener" href="http://localhost:8080/heap%EF%BC%8C%E7%BB%93%E6%9E%9C%E5%A6%82%E4%B8%8B%EF%BC%9A">http://localhost:8080/heap，结果如下：</a></p>
<pre class=" language-java"><code class="language-java">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>OutOfMemoryError<span class="token operator">:</span> Java heap space
    at java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Arrays<span class="token punctuation">.</span><span class="token function">copyOf</span><span class="token punctuation">(</span>Arrays<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">3210</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Arrays<span class="token punctuation">.</span><span class="token function">copyOf</span><span class="token punctuation">(</span>Arrays<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">3181</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>ArrayList<span class="token punctuation">.</span><span class="token function">grow</span><span class="token punctuation">(</span>ArrayList<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">265</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>ArrayList<span class="token punctuation">.</span><span class="token function">ensureExplicitCapacity</span><span class="token punctuation">(</span>ArrayList<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">239</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>ArrayList<span class="token punctuation">.</span><span class="token function">ensureCapacityInternal</span><span class="token punctuation">(</span>ArrayList<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">231</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>ArrayList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>ArrayList<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">462</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at cc<span class="token punctuation">.</span>mrbird<span class="token punctuation">.</span>spring<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>demo<span class="token punctuation">.</span>controller<span class="token punctuation">.</span>OutOfMemoryController<span class="token punctuation">.</span><span class="token function">heapOutOfMemory</span><span class="token punctuation">(</span>OutOfMemoryController<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">21</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>classes<span class="token operator">/</span><span class="token operator">:</span>na<span class="token punctuation">]</span>
    at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>NativeMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke0</span><span class="token punctuation">(</span>Native Method<span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>NativeMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>NativeMethodAccessorImpl<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">62</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>DelegatingMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>DelegatingMethodAccessorImpl<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">43</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>Method<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>Method<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">498</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>method<span class="token punctuation">.</span>support<span class="token punctuation">.</span>InvocableHandlerMethod<span class="token punctuation">.</span><span class="token function">doInvoke</span><span class="token punctuation">(</span>InvocableHandlerMethod<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">190</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>web<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>method<span class="token punctuation">.</span>support<span class="token punctuation">.</span>InvocableHandlerMethod<span class="token punctuation">.</span><span class="token function">invokeForRequest</span><span class="token punctuation">(</span>InvocableHandlerMethod<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">138</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>web<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>mvc<span class="token punctuation">.</span>method<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>ServletInvocableHandlerMethod<span class="token punctuation">.</span><span class="token function">invokeAndHandle</span><span class="token punctuation">(</span>ServletInvocableHandlerMethod<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">105</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>mvc<span class="token punctuation">.</span>method<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>RequestMappingHandlerAdapter<span class="token punctuation">.</span><span class="token function">invokeHandlerMethod</span><span class="token punctuation">(</span>RequestMappingHandlerAdapter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">879</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>mvc<span class="token punctuation">.</span>method<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>RequestMappingHandlerAdapter<span class="token punctuation">.</span><span class="token function">handleInternal</span><span class="token punctuation">(</span>RequestMappingHandlerAdapter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">793</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>mvc<span class="token punctuation">.</span>method<span class="token punctuation">.</span>AbstractHandlerMethodAdapter<span class="token punctuation">.</span><span class="token function">handle</span><span class="token punctuation">(</span>AbstractHandlerMethodAdapter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">87</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>DispatcherServlet<span class="token punctuation">.</span><span class="token function">doDispatch</span><span class="token punctuation">(</span>DispatcherServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">1040</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>DispatcherServlet<span class="token punctuation">.</span><span class="token function">doService</span><span class="token punctuation">(</span>DispatcherServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">943</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>FrameworkServlet<span class="token punctuation">.</span><span class="token function">processRequest</span><span class="token punctuation">(</span>FrameworkServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">1006</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>FrameworkServlet<span class="token punctuation">.</span><span class="token function">doGet</span><span class="token punctuation">(</span>FrameworkServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">898</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span>HttpServlet<span class="token punctuation">.</span><span class="token function">service</span><span class="token punctuation">(</span>HttpServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">634</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>FrameworkServlet<span class="token punctuation">.</span><span class="token function">service</span><span class="token punctuation">(</span>FrameworkServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">883</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span>HttpServlet<span class="token punctuation">.</span><span class="token function">service</span><span class="token punctuation">(</span>HttpServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">741</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>core<span class="token punctuation">.</span>ApplicationFilterChain<span class="token punctuation">.</span><span class="token function">internalDoFilter</span><span class="token punctuation">(</span>ApplicationFilterChain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">231</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>core<span class="token punctuation">.</span>ApplicationFilterChain<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>ApplicationFilterChain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">166</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>tomcat<span class="token punctuation">.</span>websocket<span class="token punctuation">.</span>server<span class="token punctuation">.</span>WsFilter<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>WsFilter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">53</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>websocket<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>core<span class="token punctuation">.</span>ApplicationFilterChain<span class="token punctuation">.</span><span class="token function">internalDoFilter</span><span class="token punctuation">(</span>ApplicationFilterChain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">193</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>core<span class="token punctuation">.</span>ApplicationFilterChain<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>ApplicationFilterChain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">166</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>filter<span class="token punctuation">.</span>RequestContextFilter<span class="token punctuation">.</span><span class="token function">doFilterInternal</span><span class="token punctuation">(</span>RequestContextFilter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>web<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>filter<span class="token punctuation">.</span>OncePerRequestFilter<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>OncePerRequestFilter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">119</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>web<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>core<span class="token punctuation">.</span>ApplicationFilterChain<span class="token punctuation">.</span><span class="token function">internalDoFilter</span><span class="token punctuation">(</span>ApplicationFilterChain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">193</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
</code></pre>
<p>重启应用，访问<a target="_blank" rel="noopener" href="http://localhost:8080/metaspace%EF%BC%8C%E7%BB%93%E6%9E%9C%E5%A6%82%E4%B8%8B%EF%BC%9A">http://localhost:8080/metaspace，结果如下：</a></p>
<pre class=" language-java"><code class="language-java">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>OutOfMemoryError<span class="token operator">:</span> Metaspace
    at java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>ClassLoader<span class="token punctuation">.</span><span class="token function">defineClass1</span><span class="token punctuation">(</span>Native Method<span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>ClassLoader<span class="token punctuation">.</span><span class="token function">defineClass</span><span class="token punctuation">(</span>ClassLoader<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">756</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>ClassLoader<span class="token punctuation">.</span><span class="token function">defineClass</span><span class="token punctuation">(</span>ClassLoader<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">635</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at cc<span class="token punctuation">.</span>mrbird<span class="token punctuation">.</span>spring<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>demo<span class="token punctuation">.</span>controller<span class="token punctuation">.</span>OutOfMemoryController<span class="token punctuation">.</span><span class="token function">metaspaceOutOfMemory</span><span class="token punctuation">(</span>OutOfMemoryController<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">38</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>classes<span class="token operator">/</span><span class="token operator">:</span>na<span class="token punctuation">]</span>
    at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>NativeMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke0</span><span class="token punctuation">(</span>Native Method<span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>NativeMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>NativeMethodAccessorImpl<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">62</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>DelegatingMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>DelegatingMethodAccessorImpl<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">43</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>Method<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>Method<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">498</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>na<span class="token operator">:</span><span class="token number">1.8</span><span class="token punctuation">.</span>0_231<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>method<span class="token punctuation">.</span>support<span class="token punctuation">.</span>InvocableHandlerMethod<span class="token punctuation">.</span><span class="token function">doInvoke</span><span class="token punctuation">(</span>InvocableHandlerMethod<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">190</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>web<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>method<span class="token punctuation">.</span>support<span class="token punctuation">.</span>InvocableHandlerMethod<span class="token punctuation">.</span><span class="token function">invokeForRequest</span><span class="token punctuation">(</span>InvocableHandlerMethod<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">138</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>web<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>mvc<span class="token punctuation">.</span>method<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>ServletInvocableHandlerMethod<span class="token punctuation">.</span><span class="token function">invokeAndHandle</span><span class="token punctuation">(</span>ServletInvocableHandlerMethod<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">105</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>mvc<span class="token punctuation">.</span>method<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>RequestMappingHandlerAdapter<span class="token punctuation">.</span><span class="token function">invokeHandlerMethod</span><span class="token punctuation">(</span>RequestMappingHandlerAdapter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">879</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>mvc<span class="token punctuation">.</span>method<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>RequestMappingHandlerAdapter<span class="token punctuation">.</span><span class="token function">handleInternal</span><span class="token punctuation">(</span>RequestMappingHandlerAdapter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">793</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>mvc<span class="token punctuation">.</span>method<span class="token punctuation">.</span>AbstractHandlerMethodAdapter<span class="token punctuation">.</span><span class="token function">handle</span><span class="token punctuation">(</span>AbstractHandlerMethodAdapter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">87</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>DispatcherServlet<span class="token punctuation">.</span><span class="token function">doDispatch</span><span class="token punctuation">(</span>DispatcherServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">1040</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>DispatcherServlet<span class="token punctuation">.</span><span class="token function">doService</span><span class="token punctuation">(</span>DispatcherServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">943</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>FrameworkServlet<span class="token punctuation">.</span><span class="token function">processRequest</span><span class="token punctuation">(</span>FrameworkServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">1006</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>FrameworkServlet<span class="token punctuation">.</span><span class="token function">doGet</span><span class="token punctuation">(</span>FrameworkServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">898</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span>HttpServlet<span class="token punctuation">.</span><span class="token function">service</span><span class="token punctuation">(</span>HttpServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">634</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>FrameworkServlet<span class="token punctuation">.</span><span class="token function">service</span><span class="token punctuation">(</span>FrameworkServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">883</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>webmvc<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>http<span class="token punctuation">.</span>HttpServlet<span class="token punctuation">.</span><span class="token function">service</span><span class="token punctuation">(</span>HttpServlet<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">741</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>core<span class="token punctuation">.</span>ApplicationFilterChain<span class="token punctuation">.</span><span class="token function">internalDoFilter</span><span class="token punctuation">(</span>ApplicationFilterChain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">231</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>core<span class="token punctuation">.</span>ApplicationFilterChain<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>ApplicationFilterChain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">166</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>tomcat<span class="token punctuation">.</span>websocket<span class="token punctuation">.</span>server<span class="token punctuation">.</span>WsFilter<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>WsFilter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">53</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>websocket<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>core<span class="token punctuation">.</span>ApplicationFilterChain<span class="token punctuation">.</span><span class="token function">internalDoFilter</span><span class="token punctuation">(</span>ApplicationFilterChain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">193</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>core<span class="token punctuation">.</span>ApplicationFilterChain<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>ApplicationFilterChain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">166</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>filter<span class="token punctuation">.</span>RequestContextFilter<span class="token punctuation">.</span><span class="token function">doFilterInternal</span><span class="token punctuation">(</span>RequestContextFilter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>web<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>filter<span class="token punctuation">.</span>OncePerRequestFilter<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>OncePerRequestFilter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">119</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>web<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>core<span class="token punctuation">.</span>ApplicationFilterChain<span class="token punctuation">.</span><span class="token function">internalDoFilter</span><span class="token punctuation">(</span>ApplicationFilterChain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">193</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>core<span class="token punctuation">.</span>ApplicationFilterChain<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>ApplicationFilterChain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">166</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>tomcat<span class="token operator">-</span>embed<span class="token operator">-</span>core<span class="token operator">-</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">9.0</span><span class="token punctuation">.</span><span class="token number">33</span><span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>filter<span class="token punctuation">.</span>FormContentFilter<span class="token punctuation">.</span><span class="token function">doFilterInternal</span><span class="token punctuation">(</span>FormContentFilter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">93</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>web<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
    at org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>filter<span class="token punctuation">.</span>OncePerRequestFilter<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>OncePerRequestFilter<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">119</span><span class="token punctuation">)</span> <span class="token operator">~</span><span class="token punctuation">[</span>spring<span class="token operator">-</span>web<span class="token operator">-</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">.</span>jar<span class="token operator">:</span><span class="token number">5.2</span><span class="token punctuation">.</span><span class="token number">5</span><span class="token punctuation">.</span>RELEASE<span class="token punctuation">]</span>
</code></pre>
<p>场景构建完毕。</p>
<h3 id="导出内存映像文件"><a href="#导出内存映像文件" class="headerlink" title="导出内存映像文件"></a>导出内存映像文件</h3><p>导出内存映像文件分为异常自动导出和通过<code>jmap</code>命令导出两种方式。</p>
<h4 id="内存溢出自动导出"><a href="#内存溢出自动导出" class="headerlink" title="内存溢出自动导出"></a>内存溢出自动导出</h4><p>添加<code>-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/Users/mrbird/Desktop</code>参数，表示内存溢出时，导出Dump文件，路径为/Users/mrbird/Desktop。</p>
<p>添加后，启动应用，访问<a target="_blank" rel="noopener" href="http://localhost:8080/heap%EF%BC%8C%E6%8E%A7%E5%88%B6%E5%8F%B0%E6%89%93%E5%8D%B0%EF%BC%9A">http://localhost:8080/heap，控制台打印：</a></p>
<pre class=" language-java"><code class="language-java">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>OutOfMemoryError<span class="token operator">:</span> Java heap space
Dumping heap to <span class="token operator">/</span>Users<span class="token operator">/</span>mrbird<span class="token operator">/</span>Desktop<span class="token operator">/</span>java_pid22958<span class="token punctuation">.</span>hprof <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Heap dump file created <span class="token punctuation">[</span><span class="token number">76362786</span> bytes in <span class="token number">0.348</span> secs<span class="token punctuation">]</span>

<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre>
<p>桌面多了个java_pid22958.hprof文件。</p>
<p>重启应用，访问<a target="_blank" rel="noopener" href="http://localhost:8080/metaspace%EF%BC%8C">http://localhost:8080/metaspace，</a> 桌面多了个java_pid23003.hprof文件。</p>
<h4 id="jmap命令导出"><a href="#jmap命令导出" class="headerlink" title="jmap命令导出"></a>jmap命令导出</h4><p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163049@2x.png" alt="QQ20201110-151733@2x"></p>
<p>含义为，通过jmap命令导出dump文件，<code>format=b</code>表示导出二进制文件，<code>file=/Users/mrbird/Desktop/java_23003.hprof</code>表示导出到/Users/mrbird/Desktop/java_23003.hprof文件中，最后跟上进程号。导出后，我的桌面上多了个java_23003.hprof文件。</p>
<h3 id="JProfiler分析dump文件"><a href="#JProfiler分析dump文件" class="headerlink" title="JProfiler分析dump文件"></a>JProfiler分析dump文件</h3><p>使用JProfiler软件打开上面导出的java_pid22958.hprof文件：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163219@2x.png" alt="QQ20201110-152025@2x"></p>
<p>这一页展示了各class对应实例内存占用从大到小排列情况。点击Biggest Objects查看大文件：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163219@2x.png" alt="QQ20201110-152159@2x"></p>
<p>在最大的对象上右键选择Use Retained Objects查看对象占用情况：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163219@2x.png" alt="QQ20201110-152532@2x"></p>
<p>可以看到里面密密麻麻的都是我们不断往里添加的User对象。一个User对象的大小为112bytes，整个集合对象占了将近40M。</p>
<p>打开java_pid23003.hprof文件，和上面的步骤一样，查看Use Retained Objects：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163219@2x.png" alt="QQ20201110-153230@2x"></p>
<p>可以看到导致元空间溢出的原因是因为我们加载过多class所致。</p>
<h2 id="jstack定位死循环、死锁"><a href="#jstack定位死循环、死锁" class="headerlink" title="jstack定位死循环、死锁"></a>jstack定位死循环、死锁</h2><p><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstack.html">jstack</a>可以打印出指定Java进程内部线程的堆栈信息。</p>
<p>因为下面示例需要使用到top命令，该命令在Mac、Linux上有些差异，所以下面例子选择在Linux上演示，我的虚拟机IP为192.168.33.10。</p>
<p>在上面的SpringBoot项目中，新建一个LoopController：</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@RestController</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">LoopController</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"loop"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">loop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p><code>loop</code>方法里制造死循环，启动项目，访问<a target="_blank" rel="noopener" href="http://192.168.33.10:8080/loop%E5%90%8E%EF%BC%8C%E4%BD%BF%E7%94%A8%60top%60%E5%91%BD%E4%BB%A4%E6%9F%A5%E7%9C%8B%E8%BF%9B%E7%A8%8B%E6%83%85%E5%86%B5%EF%BC%9A">http://192.168.33.10:8080/loop后，使用`top`命令查看进程情况：</a></p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163219@2x.png" alt="QQ20201111-141014@2x"></p>
<p>可以看到，进程ID为5806的进程CUP使用率飙到了100%，这个进程就是我们的boot-app：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201109-163219@2x.png" alt="QQ20201111-141511@2x"></p>
<p>然后接着使用<code>top -p 5806 -H</code>查看5806进程内部各线程情况：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-143639@2x.png" alt="QQ20201111-141118@2x"></p>
<p>可以看到占用CPU资源的线程的ID为5830。因为通过<code>jstack</code>查看Java应用线程时，线程ID为16进制格式，所以我们需要将5830转换为16进制数：</p>
<pre><code>printf "%x" 5830
</code></pre>
<p>结果为16c6。使用<code>jstack 5806 &gt; 5806.txt</code>命令将5806进程的Java线程情况导出到5806.txt文件中，导出后，通过vim命令搜索16c6，结果如下所示：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-143639@2x.png" alt="QQ20201111-141251@2x"></p>
<p>可以看到，占用CPU的线程为http-nio-8080-exec-1，对应出问题的代码为cc.mrbird.spring.boot.demo.controller.LoopController.loop的第11行。</p>
<p>死循环问题定位完毕后，我们再新建一个LockController来模拟死锁情况：</p>
<pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@RestController</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DeadLockController</span> <span class="token punctuation">{</span>

    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> Object lock1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> Object lock2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"lock"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> String <span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span>
            <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>lock1<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>lock2<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"thread1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">"thread1"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span>
            <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>lock2<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>lock1<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"thread2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">"thread2"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"0000"</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            TimeUnit<span class="token punctuation">.</span>SECONDS<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>启动项目，访问<a target="_blank" rel="noopener" href="http://192.168.33.10:8080/lock%EF%BC%8C%E7%84%B6%E5%90%8E%E4%BD%BF%E7%94%A8jstatck%E5%91%BD%E4%BB%A4%E6%9F%A5%E7%9C%8BJava%E7%BA%BF%E7%A8%8B%E6%83%85%E5%86%B5%EF%BC%9A">http://192.168.33.10:8080/lock，然后使用jstatck命令查看Java线程情况：</a></p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-143639@2x.png" alt="QQ20201111-143639@2x"></p>
<p>可以看到存在一个死锁，thread1和thread2线程互相等待锁获取。</p>
<h2 id="jvisualvm监控"><a href="#jvisualvm监控" class="headerlink" title="jvisualvm监控"></a>jvisualvm监控</h2><p><a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jvisualvm.html">jvisualvm</a>是JDK自带的监控Java进程的图形化工具，命令行输入<code>jvisualvm</code>即可启动。</p>
<h3 id="监控本地进程"><a href="#监控本地进程" class="headerlink" title="监控本地进程"></a>监控本地进程</h3><p>启动jvisualvm，在本地列表里可以看到本机正在运行的Java进程：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-143639@2x.png" alt="QQ20201111-160059@2x"></p>
<p>双击pid为41202的boot应用进程：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-143639@2x.png" alt="QQ20201111-160259@2x"></p>
<p>各tab功能：</p>
<ul>
<li>概述：显示JDK版本、进程ID，JVM参数配置等基础信息；</li>
<li>监视：以图形化界面显示CPU使用率，内存使用情况，类加载信息，线程信息等，同时可以手动触发GC，导出堆内存Dump等；</li>
<li>线程：当前进程的Java线程列表，同时也可以导出线程Dump文件；</li>
<li>抽样器：可以对当前进程的CPU使用率内存占用情况进行抽样分析；</li>
<li>Profiler：对抽样进行性能分析；</li>
<li>Visual GC：安装的一个插件，用于图形化显示堆内存情况。</li>
</ul>
<p>个人常用的功能有监视页面的内存Dump和线程页面的线程Dump，它们功能和上面介绍的jmap、jstack类似。</p>
<p>浏览器访问<a target="_blank" rel="noopener" href="http://localhost:8080/heap%E5%90%8E%EF%BC%8C%E7%82%B9%E5%87%BB%E7%9B%91%E8%A7%86%E9%A1%B5%E9%9D%A2%E7%9A%84%E2%80%9C%E5%A0%86Dump%E2%80%9D%E6%8C%89%E9%92%AE%EF%BC%9A">http://localhost:8080/heap后，点击监视页面的“堆Dump”按钮：</a></p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-143639@2x.png" alt="QQ20201111-161417@2x"></p>
<p>然后在内存占用较大的User类上右键选择在实例视图中显示：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-141511@2x.png" alt="QQ20201111-161558@2x"></p>
<p>可以看到通过引用追踪，我们可以定位到内存溢出的地方为OutOfMemoryController的data成员变量，类型为ArrayList，内部使用Object数组装载User。这个功能和上面使用JProfiler解析Dump文件追踪内存泄露原因类似。</p>
<p>除了追踪内存问题，我们可以通过点击线程页面的线程Dump按钮导出线程Dump文件来分析进程里的Java线程情况，这和jstack功能类似。</p>
<p>重启boot应用，重新使用jvisualvm连接，访问<a target="_blank" rel="noopener" href="http://localhost:8080/loop%EF%BC%9A">http://localhost:8080/loop：</a></p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-141511@2x.png" alt="QQ20201111-162428@2x"></p>
<p>可以看到名称为http-nio-8080-exec-1的线程CPU使用率为100%，点击线程Dump按钮导出Dump文件：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-141511@2x.png" alt="QQ20201111-162559@2x"></p>
<p>通过线程名称定位到问题代码。</p>
<p>重启boot应用，重新使用jvisualvm连接，访问<a target="_blank" rel="noopener" href="http://localhost:8080/lock%EF%BC%9A">http://localhost:8080/lock：</a></p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-141511@2x.png" alt="QQ20201111-162749@2x"></p>
<p>可以看到jvisualvm检查到了死锁，通过dump文件分析死锁原因：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-141511@2x.png" alt="QQ20201111-162831@2x"></p>
<p>除了导出Dump文件外，jvisualvm也可以导入上面我们通过jmap命令导出的Dump文件，这里就不演示了。</p>
<h3 id="监控远程进程"><a href="#监控远程进程" class="headerlink" title="监控远程进程"></a>监控远程进程</h3><p>远程运行的Java进程也可以通过jvisualvm监控。以在虚拟机192.168.33.10中运行的boot应用为例，要进行远程监控，需要在启动命令上配置jmx配置：</p>
<pre class=" language-java"><code class="language-java">nohup java <span class="token operator">-</span>Dcom<span class="token punctuation">.</span>sun<span class="token punctuation">.</span>management<span class="token punctuation">.</span>jmxremote \
<span class="token operator">-</span>Dcom<span class="token punctuation">.</span>sun<span class="token punctuation">.</span>management<span class="token punctuation">.</span>jmxremote<span class="token punctuation">.</span>port<span class="token operator">=</span><span class="token number">8081</span> \
<span class="token operator">-</span>Dcom<span class="token punctuation">.</span>sun<span class="token punctuation">.</span>management<span class="token punctuation">.</span>jmxremote<span class="token punctuation">.</span>ssl<span class="token operator">=</span><span class="token boolean">false</span> \
<span class="token operator">-</span>Dcom<span class="token punctuation">.</span>sun<span class="token punctuation">.</span>management<span class="token punctuation">.</span>jmxremote<span class="token punctuation">.</span>authenticate<span class="token operator">=</span><span class="token boolean">false</span> \
<span class="token operator">-</span>Djava<span class="token punctuation">.</span>net<span class="token punctuation">.</span>preferIPv4Stack<span class="token operator">=</span><span class="token boolean">true</span> \
<span class="token operator">-</span>Djava<span class="token punctuation">.</span>rmi<span class="token punctuation">.</span>server<span class="token punctuation">.</span>hostname<span class="token operator">=</span><span class="token number">192.168</span><span class="token punctuation">.</span><span class="token number">33.10</span> \
<span class="token operator">-</span>Xms200m <span class="token operator">-</span>Xmx200m \
<span class="token operator">-</span>jar boot<span class="token operator">-</span>app<span class="token punctuation">.</span>jar <span class="token operator">&amp;</span>
</code></pre>
<ul>
<li><code>-Dcom.sun.management.jmxremote</code>：表示开启jmx远程连接；</li>
<li><code>-Dcom.sun.management.jmxremote.port=8081</code>：表示jmx端口号为8081；</li>
<li><code>-Dcom.sun.management.jmxremote.ssl=false</code>：放了方便，这里不使用SSL连接；</li>
<li><code>-Dcom.sun.management.jmxremote.authenticate=false</code>：为了方便，这里不使用用户名密码认证；</li>
<li><code>-Djava.net.preferIPv4Stack=true</code>：表示使用ipv4；</li>
<li><code>-Djava.rmi.server.hostname=192.168.33.10</code>：指定主机地址。</li>
</ul>
<p>配置好后，运行该脚本启动boot项目，然后在jvisualvm的远程列表中添加远程主机：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-141511@2x.png" alt="QQ20201111-164501@2x"></p>
<p>然后添加jmx连接：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-141118@2x.png" alt="QQ20201111-164549@2x"></p>
<p>连接成功后，就可以通过jvisualvm监控远程Java进程了：</p>
<p><img src="https://gitee.com/CodeJourney/source_file/raw/master/blog_img/QQ20201111-141118@2x.png" alt="QQ20201111-164625@2x"></p>
<h2 id="附录1-Dump文件中线程状态"><a href="#附录1-Dump文件中线程状态" class="headerlink" title="附录1 - Dump文件中线程状态"></a>附录1 - Dump文件中线程状态</h2><table>
<thead>
<tr>
<th align="left">Thread State</th>
<th align="left">Description</th>
</tr>
</thead>
<tbody><tr>
<td align="left">NEW</td>
<td align="left">The thread has not yet started.</td>
</tr>
<tr>
<td align="left">RUNNABLE</td>
<td align="left">The thread is executing in the JVM.</td>
</tr>
<tr>
<td align="left">BLOCKED</td>
<td align="left">The thread is blocked waiting for a monitor lock.</td>
</tr>
<tr>
<td align="left">WAITING</td>
<td align="left">The thread is waiting indefinitely for another thread to perform a particular action.</td>
</tr>
<tr>
<td align="left">TIMED_WAITING</td>
<td align="left">The thread is waiting for another thread to perform an action for up to a specified waiting time.</td>
</tr>
<tr>
<td align="left">TERMINATED</td>
<td align="left">The thread has exited.</td>
</tr>
</tbody></table>
<p><a target="_blank" rel="noopener" href="https://mrbird.cc/Java%20Troubleshooting%E5%9F%BA%E7%A1%80.html">参考博客</a></p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">八戒取经路</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://codejourney.gitee.io/2021/04/17/Java-%E6%95%85%E9%9A%9C%E8%AF%8A%E6%96%AD%E5%AE%9E%E6%88%98/">https://codejourney.gitee.io/2021/04/17/Java-%E6%95%85%E9%9A%9C%E8%AF%8A%E6%96%AD%E5%AE%9E%E6%88%98/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">八戒取经路</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/Java/">
                                    <span class="chip bg-color">Java</span>
                                </a>
                            
                                <a href="/tags/JVM/">
                                    <span class="chip bg-color">JVM</span>
                                </a>
                            
                                <a href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
                                    <span class="chip bg-color">性能优化</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2021/04/18/Java-%E7%BA%BF%E4%B8%8A%E5%B8%B8%E8%A7%81%E6%95%85%E9%9A%9C%E9%97%AE%E9%A2%98%E6%8E%92%E6%9F%A5%E5%AE%9E%E6%88%98/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/0.jpg" class="responsive-img" alt="Java 线上常见故障问题排查实战">
                        
                        <span class="card-title">Java 线上常见故障问题排查实战</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-04-18
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            八戒取经路
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Java/">
                        <span class="chip bg-color">Java</span>
                    </a>
                    
                    <a href="/tags/%E6%AD%BB%E9%94%81/">
                        <span class="chip bg-color">死锁</span>
                    </a>
                    
                    <a href="/tags/top/">
                        <span class="chip bg-color">top</span>
                    </a>
                    
                    <a href="/tags/vmstat/">
                        <span class="chip bg-color">vmstat</span>
                    </a>
                    
                    <a href="/tags/%E8%B0%83%E4%BC%98/">
                        <span class="chip bg-color">调优</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2021/04/15/%E3%80%90%E5%AE%9E%E6%88%98%E3%80%91Zookeeper%E5%AE%9E%E6%88%98/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/9.jpg" class="responsive-img" alt="【实战】Zookeeper实战">
                        
                        <span class="card-title">【实战】Zookeeper实战</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-04-15
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            八戒取经路
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Zookeeper/">
                        <span class="chip bg-color">Zookeeper</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="503838841"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2019-2021</span>
            
            <span id="year">2019</span>
            <a href="/about" target="_blank">八戒取经路</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/" target="_blank">Matery</a>
            <br>
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/blinkfox" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:843355928@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=843355928" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 843355928" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
